﻿// Study1016.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// 第十章 STL和泛型设计

#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
using namespace std;

struct Car
{
	int id;
	Car(int id)
	{
		this->id = id;
	}
	void display() const
	{
		cout << "car" << id << endl;
	}
};

int main()
{
	vector<Car*> pcars;
	vector<Car> cars;
	pcars.reserve(5);
	for (int i = 0; i < 5; ++i)
	{
		pcars.push_back(new Car(i));
	}
	for (int i = 5; i < 10; ++i)
	{
		cars.push_back(Car(i));
	}

	cout << "elements in pcars:" << endl;
	for_each(pcars.begin(), pcars.end(), mem_fun(&Car::display));
	cout << endl;

	cout << "elements in cars:" << endl;
	for_each(cars.begin(), cars.end(), mem_fun_ref(&Car::display));
	cout << endl;

	for ( const auto& pcar:pcars)
	{
		delete pcar;
	}

	return 0;
}

